import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login'
import profile from '../views/proFile'
import Home from '../layout/layout'
import personSet from '../views/PersonalHomePage'
import axios from "../axios";
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    redirect:'/profile',
    children: [
      {
        path:'/profile',
        name:'profile',
        component:profile
      },{
        path:'/person',
        name:'person',
        component: () => import('../views/Person')
      },
      {
        path:'/personSet',
        name:'person',
        component:personSet,
        redirect:'/personInfo',
        children: [
          {
            path:'/personInfo',
            name:'personInfo',
            component: () => import('../views/PersonInfo')
          },
          {
            path:'/passwordProtection',
            name:'passwordProtection',
            component: () => import('../views/passwordProtection')
          },
          {
            path:'/changeName',
            name:'changeName',
            component: () => import('../views/changeName')
          },
          {
            path:'/changePassword',
            name:'changePassword',
            component: () => import('../views/changePassword')
          }
        ]
      },
    ]
  },
  {
    path: '/Login',
    name: 'login',
    component: Login
  }
  /*{
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/!* webpackChunkName: "about" *!/ '../views/About.vue')
  }*/
]

const router = new VueRouter({
  routes
})
/*router.beforeEach((to, from, next) => {

  axios.get("/menu/nav",{
    headers: {
      Authorization: localStorage.getItem("token")
    }
  }).then(res => {
    console.log(res.data.data)

    // 拿到menuList
    store.commit("setMenuList", res.data.data.nav)

    // 拿到用户权限
    store.commit("setPermList", res.data.data.authoritys)

    console.log(store.state.menus.menuList)

    // 动态绑定路由
    let newRoutes = router.options.routes

    res.data.data.nav.forEach(menu => {
      if (menu.children) {
        menu.children.forEach(e => {

          // 转成路由
          let route = menuToRoute(e)

          // 吧路由添加到路由管理中
          if (route) {
            newRoutes[0].children.push(route)
          }

        })
      }
    })

    console.log("newRoutes")
    console.log(newRoutes)
    router.addRoutes(newRoutes)

    hasRoute = true
    store.commit("changeRouteStatus", hasRoute)
  })
  next()
})*/
export default router
